package com.yuxin.tongyi.lsll.thread;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.pojo.LoginInfo;
import com.yuxin.tongyi.lsll.pojo.StudentInfo;
import com.yuxin.tongyi.lsll.sub.CaremaPhotoUploadActivity;
import com.yuxin.tongyi.lsll.util.FileTool;
import com.yuxin.tongyi.lsll.util.HttpRequestUtil;
import com.yuxin.tongyi.lsll.util.PubUtil;

/**
 * @author ww
 * @date:2012-4-7 上午10:10:02
 * @Desc: 获取服务器学生信息并保存
 * @version :
 * 
 */
public class SaveStudentDataThread implements Runnable {

	private Context context;
	private HttpRequestUtil request;
	private LoginInfo loginInfo;
	private String classId;
	
	public SaveStudentDataThread(Context context, LoginInfo loginInfo, String classId){
		this.context = context;
		this.loginInfo = loginInfo;
		this.classId = classId;
		
		request = new HttpRequestUtil();
	}
	
	@Override
	public void run() {
		try {
			boolean flag = request.babyListRequest(loginInfo.getLoginName(), loginInfo.getPassword(), classId); // 请求事件列表
			String status = request.getStatus();
			if (flag) {// 服务器响应
				if (status != null && !"".equals(status)) {
					Message msg = new Message();
					List<Object> objects = request.getObjList();
					if (objects != null && objects.size() > 0) { // 服务器正确响应数据
						Bundle bundle = new Bundle();
						Object obj = objects.get(0);
						if (obj instanceof StudentInfo) {
							StudentInfo studentInfo = (StudentInfo) obj;
							//将对象转换成对象流，然后通过SharedPreferences保存
							String stuInfo = FileTool.getObjectToString(studentInfo);
							//保存请求班级状态和数据
							PubUtil.setConfigValue(context, Constants.CFG_STUDENT_DATA, stuInfo);
							PubUtil.setConfigValue(context, Constants.CFG_STUDENT_STATE, Constants.TH_STUDENT_ON);
							//保存当前学生的班级
							PubUtil.setConfigValue(context, Constants.CFG_STUDENT_CLASSID, classId);
						}
					} else { 
						PubUtil.setConfigValue(context, Constants.CFG_STUDENT_STATE, Constants.TH_STUDENT_EMPTY);
					}
				} else {
					PubUtil.setConfigValue(context, Constants.CFG_STUDENT_STATE, Constants.TH_STUDENT_ERROR);
				}
			} else { // 访问服务器失败
				PubUtil.setConfigValue(context, Constants.CFG_STUDENT_STATE, Constants.TH_STUDENT_OFF);
			}
		} catch (Exception ex) {
			PubUtil.setConfigValue(context, Constants.CFG_STUDENT_STATE, Constants.TH_STUDENT_OFF);
		}
	}

}
